package co.fitstart.admin.web.controller.market;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.support.Result;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.resolver.Preloading;

import co.fitstart.admin.service.MarketCoursewareService;
import co.fitstart.admin.web.interceptor.MenuSetting;
import co.fitstart.entity.market.MarketCourse;
import co.fitstart.entity.market.template.MarketBatchCoursewareTemplate;
import co.fitstart.entity.market.template.MarketCoursewareTemplate;
import co.fitstart.entity.market.template.MarketDailyCoursewareTemplate;
import co.fitstart.entity.market.template.MarketMonthlyCoursewareTemplate;

/**
 * 
 * @author dan
 *
 */
@Controller
@RequestMapping("/market-courses/{courseId}/monthly-coursewares/{months}/batch-coursewares")
@MenuSetting("menu-market")
public class MarketCoursewareController {
    
    @Autowired
    private MarketCoursewareService marketCoursewareService;
    
    private static final Logger logger = LoggerFactory.getLogger(MarketCoursewareController.class);
    
    /**
     * 
     * @param courseId
     * @param weeks
     * @return
     */
    @RequestMapping(value = "/{weeks}", method = RequestMethod.GET)
    @MenuSetting("submenu-market-course")
    public ModelAndView list(@PathVariable("courseId") Long courseId,
                             @PathVariable("months") int months,
                             @PathVariable("weeks") int weeks) {
        ModelAndView modelAndView = new ModelAndView("market/market-course/courseware/batch-courseware-index");
        
        MarketCourse course = marketCoursewareService.loadMarketCourse(courseId);
        modelAndView.addObject("marketCourse", course);
        
        MarketMonthlyCoursewareTemplate monthlyCoursewareTemplate = marketCoursewareService.loadMarketMonthlyCoursewareTemplate(course, months);
        modelAndView.addObject("monthlyCoursewareTemplate", monthlyCoursewareTemplate);
        
        MarketBatchCoursewareTemplate batchCoursewareTemplate = marketCoursewareService.loadMarketBatchCoursewareTemplate(monthlyCoursewareTemplate, weeks);
        modelAndView.addObject("batchCoursewareTemplate", batchCoursewareTemplate);
        
        modelAndView.addObject("months", months);
        modelAndView.addObject("weeks", weeks);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param batchId
     * @return
     */
    @RequestMapping(value = "/{batchId}/edit", method = RequestMethod.GET)
    public ModelAndView editBatch(@PathVariable("batchId") Long batchId,
                                  @PathVariable("months") int months) {
        ModelAndView modelAndView = new ModelAndView("market/market-course/courseware/batch-courseware-edit");
        
        MarketBatchCoursewareTemplate batchCoursewareTemplate = marketCoursewareService.loadMarketBatchCoursewareTemplate(batchId);
        modelAndView.addObject("batchCoursewareTemplate", batchCoursewareTemplate);
        
        modelAndView.addObject("months", months);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param marketCourse
     * @return
     */
    @RequestMapping(value = "/{batchId}", method = RequestMethod.PATCH)
    public @ResponseBody String onEditBatch(@Preloading("batchId") MarketBatchCoursewareTemplate batchCoursewareTemplate,
                                            @PathVariable("months") int months) {
        Result result = new Result();
        
        try {
            
            marketCoursewareService.editMarketBatchCoursewareTemplate(batchCoursewareTemplate);
            
            result.success().message("修改每周课件成功");
            
        } catch (Exception e) {
            
            logger.error("onEditBatch - ", e);
            result.fail().message("修改每周课件失败 ");
            
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    @RequestMapping(value = "/{batchId}/daily-coursewares/{dailyCoursewareId}/edit", method = RequestMethod.GET)
    public ModelAndView editDaily(@PathVariable("courseId") Long courseId,
                                  @PathVariable("months") int months,
                                  @PathVariable("batchId") Long batchId,
                                  @PathVariable("dailyCoursewareId") Long dailyCoursewareId){
        
        ModelAndView modelAndView = new ModelAndView("market/market-course/courseware/daily-courseware-edit");
        
        MarketDailyCoursewareTemplate dailyCoursewareTemplate = marketCoursewareService.loadMarketDailyCoursewareTemplate(dailyCoursewareId);
        modelAndView.addObject("dailyCoursewareTemplate", dailyCoursewareTemplate);
        
        modelAndView.addObject("courseId", courseId);
        modelAndView.addObject("months", months);
        modelAndView.addObject("batchId", batchId);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param courseId
     * @param batchId
     * @param dailyCoursewareId
     * @param coursewareTemplate
     * @return
     */
    @RequestMapping(value = "/{batchId}/daily-coursewares/{dailyCoursewareId}", method = RequestMethod.PATCH)
    public @ResponseBody String onEditDaily(@PathVariable("courseId") Long courseId,
                                            @PathVariable("months") int months,
                                            @PathVariable("batchId") Long batchId,
                                            @Preloading("dailyId") MarketDailyCoursewareTemplate dailyCoursewareTemplate){
        
        Result result = new Result();
        
        try{
            
            marketCoursewareService.editMarketDailyCoursewareTemplate(dailyCoursewareTemplate);
            
            result.success().message("修改每日课件信息成功！");
        } catch (Exception e) {
            
            logger.error("onEditDaily - ", e);
            result.fail().message("修改每日课件信息失败！");
        } 
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * 
     * @param courseId
     * @param batchId
     * @param dailyCoursewareId
     * @return
     */
    @RequestMapping(value = "/{batchId}/daily-coursewares/{dailyCoursewareId}/coursewares/create", method = RequestMethod.GET)
    public ModelAndView create(@PathVariable("courseId") Long courseId, 
                               @PathVariable("months") int months,
                               @PathVariable("batchId") Long batchId,
                               @PathVariable("dailyCoursewareId")int dailyCoursewareId){
        
        ModelAndView modelAndView = new ModelAndView("market/market-course/courseware/courseware-create");
        
        modelAndView.addObject("courseId", courseId);
        modelAndView.addObject("months", months);
        modelAndView.addObject("batchId", batchId);
        modelAndView.addObject("dailyCoursewareId", dailyCoursewareId);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param courseId
     * @param batchId
     * @param dailyCoursewareId
     * @param coursewareTemplate
     * @return
     */
    @RequestMapping(value = "/{batchId}/daily-coursewares/{dailyCoursewareId}/coursewares", method = RequestMethod.POST)
    public @ResponseBody String onCreate(@PathVariable("courseId") Long courseId, 
                                         @PathVariable("months") int months,
                                         @PathVariable("batchId") Long batchId,
                                         @PathVariable("dailyCoursewareId") Long dailyCoursewareId,
                                         MarketCoursewareTemplate coursewareTemplate){
        
        Result result = new Result();
        
        try{
            
            MarketDailyCoursewareTemplate dailyCourseware = marketCoursewareService.loadMarketDailyCoursewareTemplate(dailyCoursewareId);
            
            marketCoursewareService.createMarketCoursewareTemplate(coursewareTemplate, dailyCourseware);
            
            result.success().message("添加动作成功！");
        } catch (Exception e) {
            
            logger.error("onCreate - ", e);
            result.fail().message("添加动作失败！");
        } 
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * 
     * @param courseId
     * @param batchId
     * @param dailyCoursewareId
     * @param coursewareId
     * @return
     */
    @RequestMapping(value = "/{batchId}/daily-coursewares/{dailyCoursewareId}/coursewares/{coursewareId}/edit", method = RequestMethod.GET)
    public ModelAndView editBatch(@PathVariable("courseId") Long courseId,
                             @PathVariable("months") int months,
                             @PathVariable("batchId") Long batchId,
                             @PathVariable("dailyCoursewareId") Long dailyCoursewareId,
                             @PathVariable("coursewareId") Long coursewareId){
        
        ModelAndView modelAndView = new ModelAndView("market/market-course/courseware/courseware-edit");
        
        MarketCoursewareTemplate courseware = marketCoursewareService.loadMarketCoursewareTemplate(coursewareId);
        modelAndView.addObject("courseware", courseware);
        
        modelAndView.addObject("courseId", courseId);
        modelAndView.addObject("months", months);
        modelAndView.addObject("batchId", batchId);
        modelAndView.addObject("dailyCoursewareId", dailyCoursewareId);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param courseId
     * @param batchId
     * @param dailyCoursewareId
     * @param coursewareTemplate
     * @return
     */
    @RequestMapping(value = "/{batchId}/daily-coursewares/{dailyCoursewareId}/coursewares/{coursewareId}", method = RequestMethod.PATCH)
    public @ResponseBody String onEditBatch(@PathVariable("courseId") Long courseId,
                                       @PathVariable("months") int months,
                                       @PathVariable("batchId") Long batchId,
                                       @PathVariable("dailyCoursewareId") Long dailyCoursewareId,
                                       @Preloading("coursewareId") MarketCoursewareTemplate coursewareTemplate){
        
        Result result = new Result();
        
        try{
            
            marketCoursewareService.editMarketCoursewareTemplate(coursewareTemplate);
            
            result.success().message("修改动作成功！");
        } catch (Exception e) {
            
            logger.error("onEditBatch - ", e);
            result.fail().message("修改动作失败！");
        } 
        
        return JsonUtils.toJsonString(result);
    }
    
    
    /**
     * 
     * @param courseId
     * @param batchId
     * @param dailyCoursewareId
     * @param coursewareId
     * @return
     */
    @RequestMapping(value = "/{batchId}/daily-coursewares/{dailyCoursewareId}/coursewares/{coursewareId}/delete", method = RequestMethod.GET)
    public ModelAndView delete(@PathVariable("courseId") Long courseId,
                               @PathVariable("months") int months,
                               @PathVariable("batchId") Long batchId,
                               @PathVariable("dailyCoursewareId") Long dailyCoursewareId,
                               @PathVariable("coursewareId") Long coursewareId){
        
        ModelAndView modelAndView = new ModelAndView("market/market-course/courseware/courseware-delete");
        
        modelAndView.addObject("courseId", courseId);
        modelAndView.addObject("months", months);
        modelAndView.addObject("batchId", batchId);
        modelAndView.addObject("dailyCoursewareId", dailyCoursewareId);
        modelAndView.addObject("coursewareId", coursewareId);
        
        return modelAndView;
    }
    
    

    /**
     * 
     * @param courseId
     * @param batchId
     * @param dailyCoursewareId
     * @param coursewareTemplate
     * @return
     */
    @RequestMapping(value = "/{batchId}/daily-coursewares/{dailyCoursewareId}/coursewares/{coursewareId}", method = RequestMethod.DELETE)
    public @ResponseBody String onDelete(@PathVariable("courseId") Long courseId,
                                         @PathVariable("months") int months,
                                         @PathVariable("batchId") Long batchId,
                                         @PathVariable("dailyCoursewareId") Long dailyCoursewareId,
                                         @Preloading("coursewareId") MarketCoursewareTemplate coursewareTemplate){
        
        Result result = new Result();
        
        try{
            
            marketCoursewareService.deleteMarketCoursewareTemplate(coursewareTemplate);
            
            result.success().message("删除动作成功！");
        }
        catch (Exception e) {
            
            logger.error("onDelete - ", e);
            result.fail().message("删除动作失败！");
        } 
        
        return JsonUtils.toJsonString(result);
    }
    

}
